package com.mjh.task;

import java.util.HashMap;
import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import com.mjh.manager.*;
import com.mjh.util.FreeMarkerUtil;
import com.mjh.comm.*;

public class TopClickMaker implements Job{
	
	private static final String TOP_CLICK_Template = "WEB-INF/templates/topClick.html";
	private static final String outputFile = Constant.SERVLET_CONTEXT + "/inc/topClick.html";
	
	private static Logger log = Logger.getLogger(TopClickMaker.class);
	
	private ArticleManager manager = new ArticleManager();
	
	public void execute(JobExecutionContext context) throws JobExecutionException {
		
		String jobName = context.getJobDetail().getName();
		TaskManager.getInstance().setTaskLastExcuteDate(jobName, context.getFireTime());
		TaskManager.getInstance().setTaskNextExcuteDate(jobName, context.getNextFireTime());
	
		//显示所有文章中点击最多的前10篇文章
		
		HashMap<String,Object> parameter = new HashMap<String, Object>();
		parameter.put("topClickList", manager.listTopClick(10));
		int result = FreeMarkerUtil.processToFile(parameter, TOP_CLICK_Template, outputFile);
		TaskManager.getInstance().setTaskLastExcuteState(jobName, result);
		
		log.info("生成点击最多的文章列表 " + Constant.OPERATE_DESC[result]);
	}

}
